<template>
  <div class="alarmConfiguration zy-content">
    <!-- 任务执行 -->
    <div class="searchBox search-box-layout">
      <search :columns="searchData" @getSearch="getSearch(arguments)"></search>
    </div>
    <div class="alarmConfiguration">
      <div class="list-layout mt16">
        <!-- 表格 -->
        <div class="btns clear">
          <span class="title">{{ $t("common.listInformation") }}</span>
          <Button @click="handleAdd">新增</Button>
        </div>
        <div class="tableContent">
          <!--            仓位管理表-->
          <Table class="resizable-table" border @on-column-width-resize="onColumnWidthResize" :columns="columns"
                 :height="tableHeight"
                 :highlight-row="true"
                 @on-row-click="handleRowClick"
                 ellipsis ref="selection" stripe :data="infoList">
            <template slot-scope="{ row }" slot="action">
            <span class="operationBtn" @click="handleEditRow(row)"
            ><i class="iconfont zy-common-bianji"></i>编辑</span
            >
              <span class="operationBtn" @click="handleDeleteRow(row)"
              ><i class="iconfont zy-common-shanchu"></i>删除</span
              >
            </template>
          </Table>
        </div>
      </div>
    </div>
    <Modal
        :mask-closable="false"
        v-model="deleteShow"
        title="删除"
        class="modal-title-layout"
    >
      <div class="icon red">
        <i class="iconfont zy-common-shanchu"></i>
      </div>
      <p class="title">
        <span>是否确认删除?</span>
      </p>
      <div slot="footer">
        <Button type="default" @click="deleteShow = false">取消</Button>
        <Button type="primary" @click="handleDeleteSure">确定</Button>
      </div>
    </Modal>
    <!-- <CreateOrUpdateModal
        :visible="createStatus"
        :form="wareForm"
        :title="wareForm.id ? '编辑' : '新增'"
        :handleAddSubmit="handleAddSubmit"
        :closeSubmit="closeSubmit"
        :typeList="typeList"
    /> -->
  </div>
</template>
<script>

import {
  getWare
} from "@/api/stock/stock.js";
import search from "@/components/curd/searchList.vue";
import toggleTableOperation from "@/mixins/toggleTableOperation";
import {basedataEquipmentAllApi} from "@/api/dataBasic/process";

export default {
  name: "stockData", //仓库配置
  components: {search},
  mixins: [toggleTableOperation],
  data() {
    return {
      rowWarehouseCode: 12,
      pageSearch: {
        current: 1,
        pageSize: 10
      },
      rowInfo: {},//左侧单击行数据
      currentItem: {},
      deleteShow: false,
      deleteShowStorage: false,
      wareCodeList: [],
      infoList: [],
      storageList: [],
      wareForm: {
        list: []
      },
      storageForm: {},
      createStatus: false,
      storageCreateStatus: false,
      //一级搜索
      searchData: [
        {
          title: "储位名称",
          key: "wareStorage",
        },
        {
          title: "储位编码",
          key: "storageCode"
        },
      ],
      //一级table配置
      columns: [
        // {
        //   key: "selection",
        //   type: "selection",
        //   width: 60,
        //   align: "center",
        //   fixed: 'left'
        // },
        {
          title: '序号',
          width: 80,
          align: 'center',
          render: (h, params) => {
            return h('span', params.index + (this.pageNum - 1) * this.pageSize + 1);
          },
        },
        {
          title: "任务单号",
          key: "code",
          minWidth: 150,
          resizable: true,
          align: "center",
        },
        {
          title: "业务类型",
          key: "transferType",
          minWidth: 150,
          align: "center",
          resizable: true,
          tooltip: true
        },
        {
          title: "出库储位",
          key: "sourceStorageId",
          minWidth: 150,
          align: "center",
          resizable: true,
          tooltip: true
        },
        {
          title: "入库储位",
          key: "targetStorageId",
          minWidth: 150,
          align: "center",
          resizable: true,
          tooltip: true
        },
        {
          title: "数量",
          key: "materialCount",
          minWidth: 150,
          align: "center",
          resizable: true,
          tooltip: true
        },
        {
          title: "创建人",
          key: "createUser",
          minWidth: 150,
          align: "center",
          resizable: true,
          tooltip: true
        },
        // {
        //   title: "入库储位",
        //   key: "warehouseFrozen",
        //   minWidth: 100,
        //   align: "center",
        //   resizable: true,
        //   tooltip: true,
        //   render: (h, params) => {
        //     return h('div', params.row.warehouseFrozen ? '是' : '否');
        //   }
        // },
        {
          title: "操作",
          slot: "action",
          fixed: "right",
          width: 180,
          align: "center",
        },
      ],
      moldCode: "",
      id: "",
      typeList: [
        {
          value: "transfer",
          label: '调拔'
        },
        {
          value: "instock",
          label: '入库'
        },
        {
          value: "outstock",
          label: '出库'
        }
      ],
      loading: false,

      // 一级二级弹窗显示
      create: false,
      createLink: false,
      //三级搜索参数
      searchParams: {},
      //一级页面分页参数
      pageNum: 1,
      pageSize: 10,
      total: 0,
      bomDetail: false,
      delFaultCodeShowF: false, //是否显示删除弹窗
      pageSizeOption: [10, 20, 30, 40],
      delIds: [],
      selection: [],
      createByOptions: []
    };
  },
  computed: {
    tableHeight() {
      return window.innerHeight - 350;
    },
  },
  mounted() {
    this.getList()
    this.getTableColumnsWidth(this.columns);
    this.searchList();
  },
  methods: {
    getList() {
      this.axios
          .get("/api/basedataWarehouse/queryAll", {}).then(res => {
        if (res.code == 200) {
          const list = res.data
          for (const item of list) {
            item.options = item.storageVOList
            item.value = item.id
            item.label = item.warehouseName
// 改造成上下级
            for (const items of item.storageVOList) {
              items.options = items.storageName
              items.value = items.id
              items.label = items.storageName
            }
            this.searchData[0].dicData = list

          }
        }
      })

    },
    // 右侧编辑
    handleEditRowStorage(row) {
      const list = JSON.parse(JSON.stringify(row))
      list.storageFrozen = list.storageFrozen.toString()
      this.storageForm = list;
      this.storageCreateStatus = true;
    },
    // 右侧 点击删除
    handleDeleteRowStorage(row) {
      this.currentItem = row;
      this.deleteShowStorage = true;
    },
    // 左侧点击编辑
    handleEditRow(row) {
      const list = JSON.parse(JSON.stringify(row))
      list.warehouseFrozen = list.warehouseFrozen.toString()
      this.wareForm = list
      this.createStatus = true;
    },
    // 左侧 仓储点击删除
    handleDeleteRow(row) {
      this.currentItem = row;
      this.deleteShow = true;
    },
    // 右侧 删除方法
    handleDeleteSureStorage() {
      this.axios({
        method: "DELETE",
        url: "/api/basedataStorage/delete",
        data: {ids: [this.currentItem.id]},
      }).then((res) => {
        if (res.code === 200) {
          this.currentItem = {};
          this.deleteShowStorage = false;
          this.searchStorage();
        } else {
          this.$Message.warning(res.message);
        }
      });
    },
    // 左侧 删除方法
    handleDeleteSure() {
      this.axios({
        method: "DELETE",
        url: "/api/basedataWarehouse/delete",
        data: {ids: [this.currentItem.id]},
      }).then((res) => {
        if (res.code === 200) {
          this.currentItem = {};
          this.deleteShow = false;
          this.searchList();
        } else {
          this.$Message.warning(res.message);
        }
      });
    },
    closeSubmit() {
      this.createStatus = false;
    },
    closeSubmitStorages() {
      this.storageCreateStatus = false;
    },
    // 右侧编辑
    handleAddSubmitStorages() {
      if (this.storageForm.id) {
        this.axios
            .put("/api/basedataStorage/update", this.storageForm)
            .then((res) => {
              if (res.code !== 200) {
                this.$Message.warning(res.message);
                return false
              }
              this.storageCreateStatus = false;
              this.searchStorage();
            })
            .catch((error) => {
              console.error("Error submitting form:", error);
            });
      } else {
        this.axios
            .post("/api/basedataStorage/save", this.storageForm)
            .then((res) => {
              if (res.code !== 200) {
                this.$Message.warning(res.message);
                return false
              }
              this.storageCreateStatus = false;
              this.searchStorage();
            })
            .catch((error) => {
              console.error("Error submitting form:", error);
            });
      }
    },
    handleAddSubmit(e) {
      // 新增新建保存
      const param = e
      param.details = param.list
      for (const item of param.list) {
        if (!item.materialId) {
          this.$Message.warning('请选择物料');
        }
        if (!item.materialCount || item.materialCount === 0) {
          this.$Message.warning('请输入数量');

        }
      }
      this.axios
          .post("/api/warehouseMaterialTransferOrder/save", param)
          .then((res) => {
            if (res.code !== 200) {
              this.$Message.warning(res.message);
              return false
            }
            this.createStatus = false;
            this.searchList();
          })
          .catch((error) => {
            console.error("Error submitting form:", error);
          });
    },
    // 左侧单击行
    handleRowClick(row, index) {
      this.rowInfo = row
      this.axios
          .post("/api/basedataStorage/listByPojoPage", {
            warehouseId: row.id, ...this.pageSearch
          })
          .then((res) => {
            if (res.code !== 200) {
              this.$Message.warning(res.message);
              return false
            }
            const list = JSON.parse(JSON.stringify(res.data.records))
            for (const item of list) {
              item.warehouseName = this.rowInfo.warehouseName
              item.warehouseCode = this.rowInfo.warehouseCode
            }
            this.storageList = list
            this.total = res.data.total
          })
          .catch((error) => {
            console.error("Error submitting form:", error);
          });

      // 这里可以进行其他操作，比如将row数据传递给父组件
      // this.$emit('row-selected', row);
    },
    handleAdd() {
      this.wareForm = {
        list: [{
          materialCount: 1,
          materialId: null
        }]
      };
      this.createStatus = true;
    },
    handleAddStorage() {
      if (this.rowInfo.id)
        this.storageForm = {
          warehouseId: this.rowInfo.id
        };
      this.storageCreateStatus = true;
    },
    reset() {
      this.searchParams = {};
      this.changeNum();
    },
    //分页
    changeNum() {
      this.pageNum = 1;
      this.searchList();
    },
    changePageSize(val) {
      this.pageSearch.pageSize = val;
      this.searchStorage();
    },
    changePageNum(val) {
      this.pageSearch.current = val;
      this.searchStorage();
    },
    searchStorage() {
      // this.rowInfo = row
      this.axios
          .post("/api/basedataStorage/listByPojoPage", {
            warehouseId: this.rowInfo.id, ...this.pageSearch
          })
          .then((res) => {
            if (res.code !== 200) {
              this.$Message.warning(res.message);
              return false
            }
            const list = JSON.parse(JSON.stringify(res.data.records))
            for (const item of list) {
              item.warehouseName = this.rowInfo.warehouseName
              item.warehouseCode = this.rowInfo.warehouseCode
            }
            this.storageList = list
            this.total = res.data.total
          })
    },
    getSearch(arg) {
      this.searchParams = arg[0] || {};
      this.searchList();
    },

    //查询列表
    searchList() {
      this.axios
          .post("/api/warehouseMaterialTransferOrder/getPage", {...this.pageSearch, ...this.searchParams}).then(res => {
        if (res.code === 200) {
          this.infoList = res.data.records;
          // const list = []
          // for (const item of res.data) {
          //   list.push(
          //       {
          //         label: item.warehouseCode,
          //         value: item.id
          //       }
          //   )
          // }
          // this.wareCodeList = list
        } else {
          this.$Message.warning({
            content: res.message,
            duration: 6,
            closable: true
          });
        }
      });

      let params = {
        pageNum: this.pageNum,
        pageSize: this.pageSize,
        devicesNumber: this.searchParams.devicesNumber || "2"
      };
      for (let key in this.searchParams
          ) {
        if (this.searchParams[key]) {
          this.$set(params, key, this.searchParams[key]);
        }
      }
    },
//批量删除
    delRowBetch() {
      if (this.delIds1.length == 0) {
        this.$Message.warning({
          content: `${this.$t("common.deleteItems")}`,
          duration: 6,
          closable: true
        });
        return;
      } else {
        this.delFaultCodeShowF = true;
      }
    }
    ,
//删除
    delRow(row) {
      //点击展示提示删除弹窗
      this.delFaultCodeShowF = true;
      this.delIds1 = [];
      this.delIds1.push(row.id);
    }
    ,
  }
}
;
</script>

<style scoped lang="scss">
.btn-box {
  padding: 8px 5px !important;
}

.flexTable {
  display: flex;
}

.table-div-body {
  width: 50%;
}
</style>
